from functools import reduce


# map/reduce
def fn(x, y):
    return x * 10 + y


a = reduce(fn, [1, 3, 5, 7, 9])
print(a)


# fn(fn(fn(fn(1, 3), 5), 7), 9)
# 10+3
# 130+5
# 1350+7
# 13570+9


#  filter
def is_odd(n):
    return n % 2 == 1


print(list(filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15])))


# 结果: [1, 5, 9, 15]


def not_empty(s):
    return s and s.strip()


print(list(filter(not_empty, ['A', '', 'B', None, 'C', '  '])))
# 结果: ['A', 'B', 'C']

# sorted

print(sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower, reverse=True))

L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
# 按名字排序
print(sorted(L, key=lambda x: x[0]))

# 按成绩倒叙
print(sorted(L, key=lambda x: x[1], reverse=True))
